
import java.util.Date;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.VPos;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.RowConstraints;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;

//@author A0096724W
public class EventListCell extends ListCell<Task>{
	private static final String ICON_START = "\uf04b";
	private static final String ICON_END = "\uf04d";
	private static final String ICON_URGENT = "\uf071";
	private static final String ICON_REMINDER = "\uf0f3";

	private GridPane grid = new GridPane();
	private Label icon_urgent = new Label();
	private Label icon_start = new Label();
	private Label icon_end = new Label();
	private Label icon_remind = new Label();
	private Label index_ = new Label();
	private Label description = new Label();
	private Label startTime = new Label();
	private Label endTime = new Label();
	private Label reminderTime = new Label();

	public EventListCell() {
		configureGrid();
		configureIndex();
		configureAllIcons();
		configureAllTime();
		configureDescription();
		addControlsToGrid(); 
	}

	private void configureGrid() {
		grid.setVgap(4);
		grid.setPadding(new Insets(0, 0, 10, 0));

		grid.getColumnConstraints().add(new ColumnConstraints(40)); // column 0 
		grid.getColumnConstraints().add(new ColumnConstraints(10)); // column 1 
		grid.getColumnConstraints().add(new ColumnConstraints(50)); // column 2
		grid.getColumnConstraints().add(new ColumnConstraints(10)); // column 3
		grid.getColumnConstraints().add(new ColumnConstraints(40)); // column 4
		grid.getColumnConstraints().add(new ColumnConstraints(40)); // column 5
		grid.getColumnConstraints().add(new ColumnConstraints(10)); // column 6
		grid.getColumnConstraints().add(new ColumnConstraints(50)); // column 7
		grid.getColumnConstraints().add(new ColumnConstraints(10)); // column 8
		grid.getColumnConstraints().add(new ColumnConstraints(40)); // column 9

		grid.getRowConstraints().add(new RowConstraints(100));

		grid.setHalignment(icon_urgent, HPos.CENTER);
		grid.setHalignment(icon_start, HPos.CENTER);
		grid.setHalignment(icon_end, HPos.CENTER);
		grid.setHalignment(icon_remind, HPos.CENTER);
		grid.setHalignment(index_, HPos.CENTER);

		grid.setValignment(reminderTime, VPos.CENTER);
		grid.setValignment(startTime, VPos.CENTER);
		grid.setValignment(endTime, VPos.CENTER);

		grid.getStyleClass().add("listpanel");
		//grid.setGridLinesVisible(true);
	}

	private void configureIndex() {
		index_.setFont(Font.font(null, FontWeight.BOLD, 20));
		index_.getStyleClass().add("whitefont");
	}

	private void configureIcon(Label icon, int size) {
		if (size == 12) {
			icon.getStyleClass().add("fontawesome12");
		} else {
			icon.getStyleClass().add("fontawesome15");
		}
	}

	private void configureAllIcons() {
		configureIcon(icon_urgent, 15);
		configureIcon(icon_start, 12);
		configureIcon(icon_end, 12);
		configureIcon(icon_remind, 12);
	}

	private void configureDescription() {
		description.setFont(Font.font(null, 20));
		description.setWrapText(true);
		description.getStyleClass().add("whitefont");
	}

	private void configureTime(Label timeLabel) {
		timeLabel.getStyleClass().add("whitefont");
	}


	private void configureAllTime() {
		configureTime(startTime);
		configureTime(endTime);
		configureTime(reminderTime);
	}


	private void addControlsToGrid() {
		grid.add(index_, 0, 0, 2, 1);
		grid.add(description, 2, 0, 7, 1);
		grid.add(icon_urgent, 9, 0, 1, 1);
		grid.add(icon_start, 0, 1, 1, 1);
		grid.add(startTime, 1, 1, 4, 1);
		grid.add(icon_end, 5, 1, 1, 1);
		grid.add(endTime, 6, 1, 4, 1);
		grid.add(icon_remind, 0, 2, 1, 1);
		grid.add(reminderTime, 1, 2, 9, 1);
	}

	@Override
	public void updateItem(Task task, boolean empty) {
		super.updateItem(task, empty);
		if (empty) {
			setText(null);
			setGraphic(null);
		} else {
			addContent(task);
		}
	}

	private void addContent(Task task){
		setIndex(task);
		setDescription(task);
		setAllTime(task);
		setAllIcons(task);
		setText(null);
		setGraphic(grid);
	}

	private void setIndex(Task task) {
		String indexString = task.getIndex() + "";
		index_.setText(indexString);
	}

	private void setDescription(Task task) {
		String descrip = task.getDescription();
		description.setText(descrip);
	}

	private void setTime(Date time, Label timeLabel) {
		String timedate = "none";

		if (time != null) {
			String date = time.toString();
			String day = date.substring(8, 11);
			String month = date.substring(4, 8);
			String year = date.substring(24);
			String hourMin = date.substring(10, 16);
			timedate = day + month + year + hourMin;
		}

		timeLabel.setText(timedate);
	}

	private void setAllTime(Task task) {
		Date start = task.getTimeSpan().getStart();
		setTime(start, startTime);

		Date end = task.getTimeSpan().getEnd();
		setTime(end, endTime);

		if (task.getReminder().getReminderStatus()) {
			Date remind = task.getReminder().getReminderTime();
			setTime(remind, reminderTime);
		} else {
			setTime(null, reminderTime);
		}
	}

	private void setIcon(Label icon, String iconType) {
		icon.setText(iconType);
	}

	private void setAllIcons(Task task) {
		if (task.getUrgent()) {
			setIcon(icon_urgent, ICON_URGENT);
		} else {
			setIcon(icon_urgent, null);
		}

		setIcon(icon_start, ICON_START);
		setIcon(icon_end, ICON_END);
		setIcon(icon_remind, ICON_REMINDER);
	}
}
